/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.user.core;

/**
 *
 * @author v-lascha
 */
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.user.exception.InvalidUserException;

public class UserDAO {

    private SessionFactory sessionFactory;

    /**
     * Save the UserBean in the db
     * @param user
     */
    public void saveUser(UserBean user) {
        Session session = getSessionFactory().openSession();
        try {
            Transaction tx = session.beginTransaction();
            session.save(user);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

    }

    /**
     * Get the appropriate UserBean from the db with respective to the given
     * username and password
     * @param uname
     *          The user name for user
     * @param password
     *          The password for user
     * @return
     *          The valid UserBean if credentials are correct
     * @throws org.user.exception.InvalidUserException
     *          Throw when given credentials are incorrect
     */
    public UserBean getUser(String uname, String password) throws InvalidUserException{
        Session session = getSessionFactory().openSession();
        String hqlQuery = "from UserBean where userName = ? and password = ?";
        UserBean result = null;
        
        try {
            Query query = session.createQuery(hqlQuery);
            query.setString(0, uname);
            query.setString(1, password);
            List<UserBean> userList = query.list();
            
            if (!userList.isEmpty()){
               //Since the user name is unique get the first element of the array 
               result = userList.get(0); 
            }else{
                throw new InvalidUserException() ;
            }
            
        } catch (HibernateException e) {
           throw new HibernateException(e.getMessage()) ;
        } finally {
            session.close();
        }
        return result;
    }

    /**
     * Checks for the exsisting user accounts for a given user name
     * @param uname
     *          The user name for user
     * @return
     *          True is the username is already used otherwise false
     */
    public boolean isUserExists(String uname){
        Session session = getSessionFactory().openSession();
        String hqlQuery = "from UserBean where userName = ?";
        boolean result = false;

        try {
            Query query = session.createQuery(hqlQuery);
            query.setString(0, uname);
            List<UserBean> userList = query.list();

            if (!userList.isEmpty()){
               result = true;
            }
        } catch (HibernateException e) {
           throw new HibernateException(e.getMessage()) ;
        } finally {
            session.close();
        }
        return result;
    }

    /**
     * @return Returns the sessionFactory.
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @param sessionFactory The sessionFactory to set.
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}
